
-module(aleph_dht_actions).
-include("aleph_data_interface.hrl").
-include("aleph_action_macros.hrl").

-export([do_action/2,send_ping/2,sort/1]).
-import(aleph_dht_session,[new_session/1,find_session/1,delete_session/1,insert_session/1,
         clear_sessions/0,set_answered/3,set_found/3,set_queried/3,is_queried_has_id/2,
         is_answered_has_id/2,set_finished/1,set_outstanding/2,get_outstanding/1]).
 
-import(aleph_utility,[while/2]).
?define_actions(ping);
?define_actions(pong).



     
      
        

    
send_ping(Ip,Port)->
    aleph_dht_rpc:send(Ip,Port,?REQ_PING(aleph_dht_table:root_id())).    


%%%%%%%%%%%handler%%%%%%%%%%%%%%
logger(ACC,Method,{IP,Port},Arg1)->
    io:format("Get message[~p]{~p} from ~p:~p~n",[Method,Arg1,IP,Port]).

ping_handler(ACC,_,{Ip,Port},Id)->
    new_node(Ip,Port,Id),
    ?RSP_PONG(aleph_dht_table:root_id()).  

pong_handler(ACC,_,{Ip,Port},Id)->
    new_node(Ip,Port,Id).

%%%%%%%%%%%%private function%%%%%%%%%%%%%%%
new_node(Ip,Port,Id)->
    Node=aleph_dht_node:create_node(Id,Ip,Port),
    aleph_dht_table:insert_node(Node,0).
sort(Nodes)->
    ok.
session_done(Key,Data)->
    ok.
